package org.metasketch.demo.diagrams.OFD;

import javafx.geometry.Orientation;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.ToolBar;

import org.metasketch.demo.model.ActorRole;
import org.metasketch.demo.model.OrganizationalFunction;
import org.metasketch.demo.model.OrganizationalUnit;
import org.metasketch.demo.model.TransactionKind;
import org.metasketch.diagram.javafx.graphics.DefaultLink;
import org.metasketch.editor.links.GraphicalEndConfig;
import org.metasketch.editor.links.GraphicalLinkConfig;
import org.metasketch.editor.links.LinkEndOrientation;
import org.metasketch.editor.links.LinkEndType;
import org.metasketch.editor.links.SemanticConfig;
import org.metasketch.editor.toolbox.DefaultDiagramBuilder;
import org.metasketch.editor.toolbox.DefaultSelectHandler;
import org.metasketch.editor.toolbox.SimpleRelation;
import org.metasketch.editor.toolbox.SimpleToolBaseDiagramType;
import org.metasketch.ham.model.HumanActivityRelationLink;

public class OFDDiagram extends SimpleToolBaseDiagramType {

	public OFDDiagram() {
		super("Organizational Function Diagram", new DefaultDiagramBuilder());

		registerTool("Select", null, "Select", new DefaultSelectHandler(), null);

		registerToolClasses("Actor Role", null, "ActorRole-ATD-Node",
				ActorRole.class, ActorRoleNode.class, false);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("ActorRole-ATD-Node");

		registerToolClasses("Organization Function", null,
				"OrganizationFunction-ATD-Node", OrganizationalFunction.class,
				OrganizationFunctionNode.class, true);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("OrganizationFunction-ATD-Node");
		// semantic config, source EndType, target EndType
		// registerDefaultGraphicalLink("empty link", "empty-link", null);

		// SemanticConfig cfg = new SemanticConfig();

		// EndConfig source = new EndConfig();
		// source.getTypes().add("TransactionKind-ATD-Node");
		//
		// EndConfig target = new EndConfig();
		// target.getTypes().add("ActorRole-ATD-Node");

		GraphicalLinkConfig linkConfigA = new GraphicalLinkConfig();
		linkConfigA.setSourceTypes("OrganizationFunction-ATD-Node");
		linkConfigA.setSourceFieldName("inheritedOrganizationalFunctions");
		// linkConfigA.getSource().getLinkEnd().setGraphic(LinkEndType.TRIANGLE);
		// linkConfigA.setSourceSemanticFieldName("name");
		linkConfigA.getSource().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);

		linkConfigA.setTargetTypes("OrganizationFunction-ATD-Node");
		// linkConfigA.setTargetFieldName("inheritedOrganizationalFunctions");
		// linkConfigA.getTarget().getLinkEnd().setGraphic(LinkEndType.TRIANGLE);
		// linkConfigA.setTargetSemanticFieldName("name");
		linkConfigA.getTarget().getLinkEnd()
				.setOrientation(LinkEndOrientation.CLOSEST);

		// this is to make sure there is no circular references between parent
		// and child
		// NOTE: there can still be circular references with a third class
		// only unidireccional links allowed.
		linkConfigA
				.setScript("targetSemantic.inheritedOrganizationalFunctions.remove(sourceSemantic);");

		registerGraphicalLink("inherits link", "inherits-link",
				InheritLink.class, null, linkConfigA);

		addDefaultDeleteFromDiagramGraphicalPopupMenuOption("inherits-link");

		// addGraphicalMenuEntry(
		// "inherits-link",
		// "Reverse",
		// null,
		// null,
		// "sourceSemantic.inheritedOrganizationalFunctions.remove(targetSemantic); targetSemantic.inheritedOrganizationalFunctions.add(sourceSemantic); ",
		// "graphic.source = targetGraphic; graphic.target = sourceGraphic; ");

		// no need for agraphical script since there is a specific graphical
		// class that will show arrows depending on the source and target
		// semantic objects, despite where they are in terms of source and
		// target graphical objects of the link.
		addGraphicalMenuEntry(
				"inherits-link",
				"Reverse",
				"sourceSemantic.inheritedOrganizationalFunctions.remove(targetSemantic); targetSemantic.inheritedOrganizationalFunctions.add(sourceSemantic); ");
		// "graphic.source = targetGraphic; graphic.target = sourceGraphic; ");

		// GraphicalLinkConfig linkConfigB = new GraphicalLinkConfig();
		// linkConfigB.setSourceTypes("TransactionKind-ATD-Node");
		// linkConfigB.setSourceFieldName("executedBy");
		// linkConfigB.getSource().getLinkEnd()
		// .setOrientation(LinkEndOrientation.CLOSEST);
		// linkConfigB.setTargetTypes("ActorRole-ATD-Node");
		// linkConfigB.setTargetFieldName("executes");
		// linkConfigB.getTarget().getLinkEnd()
		// .setOrientation(LinkEndOrientation.CLOSEST);
		//
		// registerDefaultGraphicalLink("execute link", "execute-link", null,
		// linkConfigB);

		// registerDefaultLink("test link", "test-link", null, linkConfigA,
		// linkConfigB);

		// registerLink("test link", "test-link", ConfigurableLink.class,null,
		// linkConfigA,
		// linkConfigB);

		// test
		// GraphicalLinkConfig linkConfigC = new GraphicalLinkConfig();
		// linkConfigC.setSourceTypes("ActorRole-ATD-Node");
		// // linkConfigC.setSourceFieldName("initiates");
		// linkConfigC.getSource().getLinkEnd()
		// .setOrientation(LinkEndOrientation.CLOSEST);
		// linkConfigC.setTargetTypes("ActorRole-ATD-Node");
		// // linkConfigC.setTargetFieldName("initiatedBy");
		// linkConfigC.getTarget().getLinkEnd()
		// .setOrientation(LinkEndOrientation.CLOSEST);
		//
		// registerDefaultGraphicalLink("actor link", "actor-link", null,
		// linkConfigC);

		// adding child/parent relations
		relationManager.register(new SimpleRelation(ActorRole.class,
				OrganizationalFunction.class, null, "containedActorRoles"));
		// relationManager.register(new SimpleRelation(
		// OrganizationalFunction.class, OrganizationalFunction.class,
		// "inheritedOrganizationalFunctions", null));

	}

	@Override
	public ToolBar createToolbar() {

		ToolBar toolBar = new ToolBar(new Label("Entities"),
				getTypeButton("Select"), getTypeButton("ActorRole-ATD-Node"),
				getTypeButton("OrganizationFunction-ATD-Node"),
				getTypeButton("inherits-link"));

		toolBar.setOrientation(Orientation.VERTICAL);

		// toolBar.set
		// TODO setup action handling

		return toolBar;
	}

}
